package com.mall.controller;

import com.alipay.api.domain.GoodsVO;
import com.github.pagehelper.PageHelper;
import com.mall.common.resp.ResponseResult;
import com.mall.controller.vo.GoodsCategoryVO;
import com.mall.entity.Goods;
import com.mall.service.GoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xyl
 * @date 2021-02-12 17:17
 */
@Api(tags = "商品接口")
@RestController
@RequestMapping("goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @PreAuthorize("hasRole('ADMIN') or hasRole('COMMON_ADMIN')")
    @ApiOperation("添加")
    @PostMapping("add")
    public ResponseResult add(@RequestBody @Validated Goods goods){
        return ResponseResult.toResponse(goodsService.add(goods));
    }

    @PreAuthorize("hasRole('ADMIN') or hasRole('COMMON_ADMIN')")
    @ApiOperation("修改")
    @PostMapping("modify")
    public ResponseResult modify(@RequestBody Goods goods){
        if(ObjectUtils.isEmpty(goods.getGoodsId())){
            return ResponseResult.error("id不能为null");
        }
        return ResponseResult.toResponse(goodsService.modifyGoodsById(goods));
    }

    @PreAuthorize("hasRole('ADMIN') or hasRole('COMMON_ADMIN')")
    @ApiOperation("删除")
    @PostMapping("remove")
    public ResponseResult remove(@RequestParam Integer goodsId){
        return ResponseResult.toResponse(goodsService.removeGoodsById(goodsId));
    }

    @ApiOperation("根据searchKey 在 '名称','介绍','详情'含有此字段的数据 , 或 根据分类查询")
    @GetMapping("search")
    public Object search(@RequestParam(required = false) String searchKey,
                                              @RequestParam(required = false) String categoryName){
        if(StringUtils.isEmpty(searchKey) && StringUtils.isEmpty(categoryName)){
            return ResponseResult.error("数据格式有误!");
        }
        return goodsService.search(searchKey,categoryName);
    }

//    @ApiOperation("根据goodsId查询商品列表 , 分页效果")
//    @GetMapping("/category/page/listVO")
//    public List<Goods> listVO(@RequestBody Goods goods){
//        return goodsService.getGoodsList(goods);
//    }

    @ApiOperation("选随机筛出几条数据,可添加在随机时,筛选值count , 和分类条件. 没有分页")
    @GetMapping("/random/list")
    public List<Goods> randomList(Goods goods){
        return goodsService.getRandomGoods(goods);
    }


    @ApiOperation("筛选值count , 和分类条件. 没有分页")
    @GetMapping("/list")
    public List<Goods> List(Goods goods){
        return goodsService.getGoodsList(goods);
    }

    @ApiOperation("按照分类查询数据")
    @GetMapping("/category/list")
    public List<GoodsCategoryVO> goodsCategoryVOS(){
        return goodsService.getGooodsByCategory();
    }

    @ApiOperation("根据goodsId获取数据")
    @GetMapping("/get")
    public ResponseResult get(@RequestParam Integer goodsId){
        return ResponseResult.success(goodsService.getGoodsById(goodsId));
    }

    @ApiOperation("所有商品")
    @GetMapping("/list/all")
    public List<Goods> listAll(){
        return goodsService.listAll();
    }

    @ApiOperation("分页查询")
    @GetMapping("/list/page")
    public List<Goods> listPage(@RequestParam Integer pageNumber,@RequestParam String categoryName,@RequestParam Integer seq){
        PageHelper.startPage(pageNumber,15);
        return goodsService.listBySeq(categoryName,seq);
    }
}